package com.nexus.all.controller;

import com.nexus.bean.exeption.ServiceException;
import com.nexus.bean.result.Result;
import com.nexus.bean.result.ResultCode;
import com.nexus.oss.service.OssService;
import com.nexus.web.annotation.RepeatSubmit;
import com.nexus.web.controller.CommonController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
@RequestMapping("oss")
@RestController
@AllArgsConstructor
@Api(tags = "oss管理接口")
public class OssController extends CommonController {

    private final OssService ossService;


    @ApiOperation(value = "上传", notes = "上传")
    @PostMapping("/upload")
    public Result upload(@ApiParam("文件") @RequestParam("file") MultipartFile file) {
        String url = null;
        try {
            url = ossService.upload(file);
        } catch (Exception e) {
            log.error("上传图片报错,异常信息:{}", e);
            throw new ServiceException(ResultCode.COMMON_MESSAGE, "请上传正确格式的照片");
        }
        return Result.newSuccess(url);
    }

    @ApiOperation(value = "根据Url上传", notes = "根据Url上传")
    @PostMapping("/uploadByUrl")
    @RepeatSubmit(interval = 1000 * 60 * 10, message = "请勿重复上传")
    public Result uploadByUrl(@ApiParam("文件") @RequestParam("file") String url) {
        String respUrl = null;
        try {
            respUrl = ossService.uploadImageByUrl(url);
        } catch (Exception e) {
            log.error("上传图片报错,异常信息:{}", e);
            throw new ServiceException(ResultCode.COMMON_MESSAGE, "请上传正确格式的照片");
        }
        return Result.newSuccess(respUrl);
    }

    @ApiOperation(value = "上传压缩图片 .jpg", notes = "上传压缩图片 .jpg")
    @PostMapping("/uploadZipImage")
    public Result uploadZipImage(@ApiParam("文件") @RequestParam("file") MultipartFile file) {
        String url = null;
        try {
            url = ossService.uploadZipImage(file);
        } catch (Exception e) {
            log.error("上传图片报错,异常信息:{}", e);
            throw new ServiceException(ResultCode.COMMON_MESSAGE, "请上传正确格式的照片");
        }
        return Result.newSuccess(url);
    }
}
